package com.xiaobawang.xjsyy_idea_plugin.factory;

import com.dtflys.forest.Forest;
import com.dtflys.forest.config.ForestConfiguration;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.*;
import com.xiaobawang.xjsyy_idea_plugin.panel.config.view.ConfigFrame;
import com.xiaobawang.xjsyy_idea_plugin.panel.dev_process.view.DevProcessFrame;
import com.xiaobawang.xjsyy_idea_plugin.global.GlobalConfig;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

/**
 * @author synhard
 * @description TODO
 * @date 2024/5/2 21:09
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
public class DevToolFactory implements ToolWindowFactory {
    /**
     * @param project    项目
     * @param toolWindow 工具窗口对象
     * @author synhard
     * @description TODO
     * @date 2024/5/2 21:10
     */
    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        addContentToToolWindow(project, toolWindow);
    }

    private void addContentToToolWindow(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        // 添加面板
        ContentManager contentManager = toolWindow.getContentManager();
        Content configFrame = contentManager.getFactory().createContent(new ConfigFrame(), "插件配置", true);
        Content devProcessFrame = contentManager.getFactory().createContent(new DevProcessFrame(), "研发流程", true);
        contentManager.addContent(configFrame);
        contentManager.addContent(devProcessFrame);

        // 添加事件监听器使用匿名内部类
        contentManager.addContentManagerListener(new ContentManagerListener() {
            @Override
            public void contentAdded(ContentManagerEvent event) {
                // 可以根据需要处理添加内容的事件
            }

            @Override
            public void contentRemoved(ContentManagerEvent event) {
                // 可以根据需要处理移除内容的事件
            }

            @Override
            public void selectionChanged(ContentManagerEvent event) {
                Content content = event.getContent();
                if (content.isSelected()) {
                    if (content.getComponent() instanceof ConfigFrame) {
                        // 配置面板
                        if (StringUtils.isEmpty(GlobalConfig.token)) {
                            ((ConfigFrame) content.getComponent()).showPreConfigDialog();
                        } else {
                            ((ConfigFrame) content.getComponent()).switchToConfigPanel();
                        }
                    } else if (content.getComponent() instanceof DevProcessFrame) {
                        // 研发流程面板
                        if (!GlobalConfig.checkAppName()) {
                            content.getComponent().removeAll();
                            Messages.showErrorDialog("请在配置面板配置正确的应用。", "错误");
                        } else if (!GlobalConfig.checkToken()) {
                            content.getComponent().removeAll();
                            Messages.showErrorDialog("请在配置面板配置正确的token。", "错误");
                        } else if (!GlobalConfig.checkGitRepo()) {
                            content.getComponent().removeAll();
                            Messages.showErrorDialog("请在配置面板配置正确的代码仓库。", "错误");
                        } else {
                            content.getComponent().removeAll();
                            ((DevProcessFrame) content.getComponent()).init();
                        }
                    }
                }
            }
        });
    }
}